package id.net.iconpln.bb.client.view.secman;

import id.net.iconpln.bb.client.service.SecmanService;
import id.net.iconpln.bb.client.service.SecmanServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbChangePassword extends LayoutContainer{
	private final SecmanServiceAsync secmanService = GWT.create(SecmanService.class);
	
	private TextField tfNamaUser, tfPasswordNew, tfPasswordNewConfirm;
	private TextField tfPasswordOriginal;

    private Button bSubmit = new Button("Submit");

	Map mapParam;

	@Override
	protected void onRender(Element parent, int index) 
	{
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());	    

	    ContentPanel cp = new ContentPanel();  

	    cp.setHeading("Ubah Password");  
	    cp.setFrame(true);  
	    cp.setSize(900, 400);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
			
	    FormPanel panel = createFormInput();
	    tfNamaUser.setRawValue((String) Registry.get("whoAmI"));

	    cp.add(panel, new RowData(.4, 1));
		
	    add(cp, new FitData(5));
	}
	
	
	// Form Panel
	private FormPanel createFormInput()
	{
		FormPanel panel = new FormPanel();
		panel.setHeaderVisible(false);
		
		FormLayout flLogin = new FormLayout();
		flLogin.setLabelWidth(150);
		
		FieldSet fsLogin = new FieldSet();
		fsLogin.setHeading("Property Login");
		fsLogin.setCollapsible(false);
		fsLogin.setLayout(flLogin);
		
		tfNamaUser = new TextField();
		tfNamaUser.setId("tfNamaUser");
		tfNamaUser.setName("tfNamaUser");
		tfNamaUser.setFieldLabel("Nama Login");
		tfNamaUser.setReadOnly(true);
		fsLogin.add(tfNamaUser);

		tfPasswordOriginal = new TextField();
		tfPasswordOriginal.setId("tfPasswordOriginal");
		tfPasswordOriginal.setName("tfPasswordOriginal");
		tfPasswordOriginal.setFieldLabel("Password Sekarang");
		tfPasswordOriginal.setPassword(true);
		tfPasswordOriginal.setMinLength(4);
		fsLogin.add(tfPasswordOriginal);
		
		tfPasswordNew = new TextField();
		tfPasswordNew.setId("tfPasswordNew");
		tfPasswordNew.setName("tfPasswordNew");
		tfPasswordNew.setFieldLabel("Password Baru");
		tfPasswordNew.setPassword(true);
		tfPasswordNew.setMinLength(4);
		fsLogin.add(tfPasswordNew);
		
		tfPasswordNewConfirm = new TextField();
		tfPasswordNewConfirm.setId("tfPasswordNewConfirm");
		tfPasswordNewConfirm.setName("tfPasswordNewConfirm");
		tfPasswordNewConfirm.setFieldLabel("Konfirmasi Password Baru");
		tfPasswordNewConfirm.setPassword(true);
		tfPasswordNewConfirm.setMinLength(4);
		fsLogin.add(tfPasswordNewConfirm);

		panel.add(fsLogin);

	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);

	    bSubmit.addListener(Events.OnClick, new SelectionListener<ButtonEvent>() {
			public void handleEvent(ButtonEvent be)
			{
				try
				{	
					if (tfPasswordOriginal.getRawValue().equals("") || tfPasswordNew.getRawValue().equals("") || tfPasswordNewConfirm.getRawValue().equals(""))
						throw new Exception("Password kosong.");
					
					if (!tfPasswordNew.getRawValue().toString().equals(tfPasswordNewConfirm.getRawValue().toString()))
						throw new Exception("Password baru tidak sama.");
					
					//update	
		    	  	mapParam = new FastMap();
		    		mapParam.put("namaLogin", tfNamaUser.getRawValue());	
		    		mapParam.put("PasswordOriginal", tfPasswordOriginal.getRawValue());	
		    		mapParam.put("PasswordNew", tfPasswordNew.getRawValue());	
		    		
					secmanService.changePassword("UPDATE", mapParam, new AsyncCallback<List<Map>>() {
						public void onFailure(Throwable caught) {
							GWT.log(caught.getMessage(), null);
						}

						public void onSuccess(List<Map> result) {
							String gethasil = result.get(0).get("gethasil").toString();
							
							if (gethasil.equalsIgnoreCase("TRUE"))
							{
								MessageBox.info("Change Password", "Berhasil Update", null);
							}
							else
							{
								MessageBox.alert("Change Password", "Gagal Update: " + gethasil, null);
							}
						}
					});
				}
				catch(Exception ex)
				{
					MessageBox.alert("Kesalahan", ex.getMessage().toString(), null);
				}
			}

			@Override
			public void componentSelected(ButtonEvent ce) {}
		});

	    p.addButton(bSubmit);

	    panel.add(p);
		return panel;
	}
}
